package com.group525.cloudef.service.admin.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group525.cloudef.common.CommonResult;
import com.group525.cloudef.entity.admin.Article;
import com.group525.cloudef.mapper.admin.ArticleMapper;
import com.group525.cloudef.service.admin.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class articleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public CommonResult<?> AddArticle(Article article) {
        //1.获取文章内容与标题
        String title = article.getTitle();
        String content = article.getContent();

        //2.1 判断非空
        if (title != null && content != null) {

            if (title.trim().equals("")) {
                return CommonResult.failed( "标题不能为空");
            }
            if (content.trim().equals("")) {
                return CommonResult.failed( "内容不能为空");
            }
            //3.1插入数据库
            int insert = articleMapper.insert(article);
            //3.2判断是否成功
            if (insert <= 0) {
                return  CommonResult.failed( "添加失败");
            }
            return CommonResult.success("添加成功");
        }
        //2.2 为空失败
        return CommonResult.failed("添加失败");


    }

    @Override
    public CommonResult<?> DeleteArticle(String id) {
        if (id != null) {
            int i = articleMapper.deleteById(id);
            if (i > 0) {
                return CommonResult.success( "删除成功");
            } else {
                return CommonResult.failed( "删除失败");
            }
        }
        return CommonResult.failed( "参数错误");
    }

    @Override
    public CommonResult<?> ModifyArticle(Article article) {
        //1.获取文章内容与标题
        String title = article.getTitle();
        String content = article.getContent();

        //2.1 判断非空
        if (title != null && content != null) {

            if (title.trim().equals("")) {
                return CommonResult.success( "标题不能为空");
            }
            if (content.trim().equals("")) {
                return CommonResult.failed( "内容不能为空");
            }
            //3.1更新数据库
            int insert = articleMapper.update(article, null);

            //3.2判断是否成功
            if (insert <= 0) {
                return CommonResult.failed("更新失败");
            }
            return CommonResult.success( "更新成功");
        }
        //2.2 为空失败
        return CommonResult.failed("更新失败");
    }
}
